package com.feng.leetcode;

import java.util.*;

/**
 * @author feng
 * @decrition
 * @date 2022/11/1312:41
 */
public class L1604 {
    public static List<String> alertNames(String[] keyName, String[] keyTime) {
        List<String> result = new ArrayList<>();
        Map<String, ArrayList<String>> map = new HashMap<>();
        for (int i = 0; i < keyName.length; i++) {
            ArrayList<String> orDefault = map.getOrDefault(keyName[i], new ArrayList<>());
            orDefault.add(keyTime[i]);
            map.put(keyName[i], orDefault);
        }
        Set<Map.Entry<String, ArrayList<String>>> entries = map.entrySet();
        Iterator<Map.Entry<String, ArrayList<String>>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, ArrayList<String>> next = iterator.next();
            ArrayList<String> value = next.getValue();
            if (value.size() < 3) {
                continue;
            }
            Collections.sort(value);
            int count = 0;
            for (int i = 2; i < value.size(); i++) {
                String one = value.get(i - 2);
                String two = value.get(i);
                String[] splitOne = one.split(":");
                String[] splitTwo = two.split(":");
                int hour1 = stringToNum(splitOne[0]);
                int hour2 = stringToNum(splitTwo[0]);
                if (hour2 - hour1 == 0) {
                    count++;
                    break;
                }
                if (Math.abs(hour2 - hour1) == 1) {
                    int second1 = stringToNum(splitOne[1]);
                    int second2 = stringToNum(splitTwo[1]);
                    int abs = second2 - second1;
                    if (abs <= 0) {
                        count++;
                        break;
                    }
                }
                if (count >= 1) {
                    break;
                }
                count = 0;
            }
            if (count >= 1) {
                result.add(next.getKey());
            }
        }
        Collections.sort(result);
        return result;
    }

    private static int stringToNum(String temp) {
        if (temp.startsWith("0")) {
            temp = temp.substring(1);
        }
        return Integer.valueOf(temp);
    }

    public static void main(String[] args) {
        String[] name = {"a","a","a","a","a","b","b","b","b","b","b"};
        String[] time = {"04:48","23:53","06:36","07:45","12:16","00:52","10:59","17:16","00:36","01:26","22:42"};
        alertNames(name, time);
    }
}
